<?php
namespace app\admin\controller;
require ROOT_PATH.'vendor'.DS.'JPush'.DS.'autoload.php';
use app\admin\controller\Base;
use JPush\Client as JPush;
use think\Db;


class Jpushtest extends Base{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function index(){
		$xiaozhu = Db::name('membership')->where('type',1)->paginate(15);
		$user = Db::name('username')->field('id,username')->where(['nbzh'=>0,'swzh'=>0])->select();
		
		$this->assign('xiaozhu',$xiaozhu);
		$this->assign('user',$user);
		return $this->fetch();
	}
	public function tuisong(){
		$uid = input('uuid');
		$beizhu = input('beizhu');
		$title = input('title');
		$user = Db::name('membership')->where('id',$uid)->find();
		if($user){
			$date = array('title'=>$title,'mid'=>$uid,'num'=>$user['num'],'fstime'=>time(),'type'=>1,'content'=>$beizhu);
			if($beizhu){
				$ids = explode(',',$user['memberid']);
				$arr = array();
				foreach($ids as $id){
					$registration_id = Db::name('username')->where('id',$id)->value('registration_id');
					$arr[] = $this->pushMessage($registration_id,$beizhu);
				}
				if(in_array(1,$arr)){
					Db::name('tuisong_jl')->insert($date);
					js_tishi('推送成功','index');
				}else{
					alert_back('失败');
				}
			}
		}
	}	
	public function history(){

		$history = Db::name('tuisong_jl')->alias('tj')->field('tj.*,ms.name')->join('__MEMBERSHIP__ ms','ms.id=tj.mid','left')->order('tj.fstime','DESC')->paginate(15);
		$this->assign('history',$history);
		return $this->fetch();
	}
	public function addMember(){
		$member_id = input('post.memberid');
		$ids = explode(',',$member_id);
		$num = count($ids);
		$date = array('name'=>input('post.name'),'num'=>$num,'type'=>1,'creattime'=>time(),'contens'=>input('post.contens'),'memberid'=>$member_id);
		if(Db::name('membership')->insert($date)){
			js_tishi('添加成功','index');
		}else{
			alert_back('失败');
		}
		
	}
	public function memberList(){
		$uid = input('uid');
		$user = Db::name('membership')->where('id',$uid)->find();
		$ids = explode(',',$user['memberid']);
		$us = array();
		foreach ($ids as $key => $id) {
			# code...
			$us[] = Db::name('username')
					->alias('a')
					->field(['a.id as uid','a.zcjje as zcjje','a.reg_je as reg_je','a.username as username','a.login_num','a.cz_num','a.tx_num','a.dl as dl','a.je as je','a.open as open','a.reg_time as reg_time','a.khd as khd','a.login_time as login_time','a.login_ip as login_ip','a.czzje as czzje','a.txzje as txzje','a.tzzje as tzzje','a.tjr as tjr','a.beizhu as beizhu','a.nbzh as nbzh','a.reg_ip as reg_ip','a.bankname as bankname'])
					->join('__US_CLASS__ b','b.id=a.us_class')
					->field(['b.class_name as hy_name'])
					->where('a.sales',0)
					->where('a.id',$id)->order('a.id desc')
					->find();
		}
		$this->assign('query',$us);
		return $this->fetch('member_list');
	}
	public function delt(){
		$uid = input('uid');
		if(Db::name('membership')->where('id',$uid)->delete()){
			return 1;
		}else{
			return 2;
		}
	}
	public function quanbu(){
		
		if(input('content')){
			$content = input('content');
			$query = $this->pushMessages($content);
			if($query==1){
				$date = array('title'=>'全部推送','mid'=>0,'num'=>0,'fstime'=>time(),'type'=>0,'content'=>$content);
				Db::name('tuisong_jl')->insert($date);
				js_tishi('推送成功','quanbu');
			}
		}
		return $this->fetch();
	}
	public function pushMessage($registration_id,$message){

		$client = new JPush('93a26a138b39f3a1bfdd50ab', '982100dbd52eed4b4351111f');
		try {
        $response = $client->push()
            ->setPlatform(array('ios', 'android'))
            //->addAllAudience()
            ->addRegistrationId($registration_id)
            ->setNotificationAlert($message)
            ->send();
        	return 1;

	    } catch (\JPush\Exceptions\APIConnectionException $e) {
	        // try something here
	        return 2;
	    } catch (\JPush\Exceptions\APIRequestException $e) {
	        // try something here
	        return 3;
	    }

    }
    public function pushMessages($message,$type=0){
		$client = new JPush('93a26a138b39f3a1bfdd50ab', '982100dbd52eed4b4351111f');
		try {
        	$response = $client->push()->addAllAudience();
			if(1==$type){
				$response->setPlatform(array('ios'))->setMessage($message)->send();
			}elseif(2==$type){
				$response->setPlatform(array('android'))->setMessage($message)->send();
			}else{
				$response->setPlatform(array('ios', 'android'))->setNotificationAlert($message)->send();
			}
        	return 1;
	    } catch (\JPush\Exceptions\APIConnectionException $e) {
	        return 2;
	    } catch (\JPush\Exceptions\APIRequestException $e) {
	        // try something here
	        return 3;
	    }

    }

	public function user_list(){
		$param = request()->param();
		$add = isset($param['add']) ? explode(',',$param['add'])  : [];
		$original = isset($param['o']) ? explode(',',$param['o']) : [];
		$all = array_unique(array_merge($add,$original));
		if(isset($param['add'])){
			if( !empty($param['add']) ){
				if( isset($param['o']) ){
					if( empty($param['o']) ){
						$param['o'] = $param['add'];
					}else{
						$param['o'] .= ','.$param['add'];
					}
				}else{
					$param['o'] = $param['add'];
				}
				$o = array_unique(explode(',',$param['o']));
				$param['o'] = implode(',',$o);
			}
			unset($param['add']);
		}

		if( isset($param['remove']) ){
			if( !empty($param['remove']) ) {
				$remove = explode(',',$param['remove']);
				if( isset($param['o']) ){
					if( !empty($param['o']) ){
						$param['o'] = explode(',',$param['o']);
						$param['o'] = implode(',',array_diff($param['o'],$remove));
					}else{
						unset($param['o']);
					}
				}
				$all = array_diff($all,$remove);
			}
			unset($param['remove']);
		}
		$user = Db::name('username')->field('id,username')->where(['nbzh'=>0,'swzh'=>0])->field("id,username")->paginate(10,false,array('query'=>$param));
		$this->assign('query',$user);
		$this->assign('select',$all);
		$this->assign('original',implode(',',$all));
		return $this->fetch();
	}
}